import router from '@/router';
import store from '@/store';
import { Message } from 'element-ui';

// 全局前置导航守卫
router.beforeEach(async(to, from, next) => {
    // 设置标题
    document.title = to.meta.title;

    // 鉴权
    await store.dispatch('permission/PMS_VALIDATE_AUTH').then(() => {
        if (store.getters['permission/PMS_GETTERS'].refresh) { // 避免页面刷新动态新增路由
            if (to.fullPath.indexOf('/admin') !== -1) {
                if (store.getters['permission/PMS_GETTERS'].refresh) {
                    store.commit('permission/PMS_SET_VALUE', { prop: 'refresh', data: false });
                }
                next(to.fullPath);
            } else {
                next(true);
            }
        }
        next(true);
    }).catch(() => {
        Message.error('鉴权异常');
    });
});